import os
import shelve

from common import Common


class Persistence:
    """本地持久化缓存
    """
    root = Common.get_root_path()
    root_cache = "cache"

    def __init__(self, database):
        self.database = os.path.join(self.root, self.root_cache, database)

    def save(self, key, value):
        db = shelve.open(self.database)
        db[key] = value
        db.close()

    def get(self, key):
        db = shelve.open(self.database)
        value = db[key]
        return value


if __name__ == '__main__':
    p_model = Persistence("test")
    p_model.save("1", {1: 1, 2: 2, 3: 3})
    a = p_model.get("1")
    print(a)
